/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package vistas;

import entidades.SalidaCombustible;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.text.SimpleDateFormat;
import java.util.List;
import javax.swing.AbstractAction;
import javax.swing.JComponent;
import javax.swing.JOptionPane;
import javax.swing.KeyStroke;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumnModel;
import reportes.GenReportes;
import rn.CombustibleRN;

/**
 *
 * @author Pablo
 */
public class FormReimprimirComprobanteSalidaCombustible extends javax.swing.JFrame {

    public FormReimprimirComprobanteSalidaCombustible() {
        initComponents();
        
        tabla.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), "Enter");
        tabla.getActionMap().put("Enter", new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent ae) {
                System.out.println("Se presiono la tecla enter");
                int fila = tabla.getSelectedRow();
                Long id = (Long) tabla.getValueAt(fila, 0);
                tf_nro_comprobante.setText(id.toString());
                                             
            }
        });
        
        getRootPane().setDefaultButton(bt_reimprimir_selecionado); 
        
        this.setLocationRelativeTo(null); 
        this.setVisible(true);
    }
    
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        tf_nro_comprobante = new javax.swing.JTextField();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tabla = new javax.swing.JTable();
        fecha_desde = new org.freixas.jcalendar.JCalendarCombo();
        jLabel3 = new javax.swing.JLabel();
        fecha_hasta = new org.freixas.jcalendar.JCalendarCombo();
        jLabel2 = new javax.swing.JLabel();
        bt_buscar = new javax.swing.JButton();
        jLabel4 = new javax.swing.JLabel();
        bt_reimprimir_ultimo = new javax.swing.JButton();
        bt_reimprimir_selecionado = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Reeimprimir Comprobante - Salida de Combustible");

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Nro. Comprobante a reimprimir"));

        jLabel1.setText("Nro. Comprobante:");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(18, 18, 18)
                .addComponent(tf_nro_comprobante, javax.swing.GroupLayout.PREFERRED_SIZE, 198, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(tf_nro_comprobante, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Bucar por rango de fechas"));

        tabla.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {},
                {},
                {},
                {}
            },
            new String [] {

            }
        ));
        jScrollPane1.setViewportView(tabla);

        jLabel3.setText("Hasta:");

        jLabel2.setText("Desde:");

        bt_buscar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/magnifier.png"))); // NOI18N
        bt_buscar.setText("Buscar");
        bt_buscar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bt_buscarActionPerformed(evt);
            }
        });

        jLabel4.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel4.setText("Seleccione fila y presione Reimprimir Comprobante");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 330, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(fecha_desde, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jLabel2))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel3)
                                    .addGroup(jPanel2Layout.createSequentialGroup()
                                        .addComponent(fecha_hasta, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addGap(0, 0, Short.MAX_VALUE)))
                                .addGap(47, 47, 47)
                                .addComponent(bt_buscar, javax.swing.GroupLayout.PREFERRED_SIZE, 103, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 547, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jLabel3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(fecha_desde, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(fecha_hasta, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(bt_buscar))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 149, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 12, Short.MAX_VALUE)
                .addComponent(jLabel4))
        );

        bt_reimprimir_ultimo.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        bt_reimprimir_ultimo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/printer.png"))); // NOI18N
        bt_reimprimir_ultimo.setText("Reimprimir último Comprobante");
        bt_reimprimir_ultimo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bt_reimprimir_ultimoActionPerformed(evt);
            }
        });

        bt_reimprimir_selecionado.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        bt_reimprimir_selecionado.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/printer.png"))); // NOI18N
        bt_reimprimir_selecionado.setText("Reimprimir Comprobante");
        bt_reimprimir_selecionado.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bt_reimprimir_selecionadoActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addContainerGap())))
            .addGroup(layout.createSequentialGroup()
                .addGap(67, 67, 67)
                .addComponent(bt_reimprimir_ultimo)
                .addGap(18, 18, 18)
                .addComponent(bt_reimprimir_selecionado)
                .addGap(0, 0, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(34, 34, 34)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(bt_reimprimir_ultimo, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(bt_reimprimir_selecionado, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void bt_buscarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bt_buscarActionPerformed
        List<SalidaCombustible> salidaCombustiblePorFecha = CombustibleRN.getSalidaCombustiblePorFecha(fecha_desde.getDate(), fecha_hasta.getDate());
        llenarTablaResultado(salidaCombustiblePorFecha);
    }//GEN-LAST:event_bt_buscarActionPerformed

    private void bt_reimprimir_selecionadoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bt_reimprimir_selecionadoActionPerformed
        
        GenReportes g = new GenReportes();
        if (!tf_nro_comprobante.getText().equals("")) {
            if (verificarValor(tf_nro_comprobante.getText())) {
                g.repSalidaCombustible(new Long(tf_nro_comprobante.getText()));
            } else {
                JOptionPane.showMessageDialog(null, "El valor ingresado es incorrecto");
            }
        } else {
            if (tabla.getSelectedRow() != -1) {
                int selectedRow = tabla.getSelectedRow();
                Long id = (Long) tabla.getModel().getValueAt(selectedRow, 0);
                g.repSalidaCombustible(id);
            }
            else
              JOptionPane.showMessageDialog(null, "Debe selecionar una fila");  
        }
        tf_nro_comprobante.setText(""); 
    }//GEN-LAST:event_bt_reimprimir_selecionadoActionPerformed

    private void bt_reimprimir_ultimoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bt_reimprimir_ultimoActionPerformed
        GenReportes g = new GenReportes();
        g.repSalidaCombustible(CombustibleRN.getIdUltimoSalida());
    }//GEN-LAST:event_bt_reimprimir_ultimoActionPerformed

    private void iniciarModeloTabla() {
        Object datos[][] = {};
        String[] columnNames = {"Nº Comp.", "Fecha", "Vehículo", "Litros cargados"};
        modelo_de_tabla = new DefaultTableModel(datos, columnNames);        
    }
  
    DefaultTableModel modelo_de_tabla;
    
    private void llenarTablaResultado(List<SalidaCombustible> resultado) {
        iniciarModeloTabla();
        SimpleDateFormat formateador = new SimpleDateFormat("dd/MM/yyyy");
        for (SalidaCombustible c : resultado) {

            Object fila[] = new Object[]{
                c.getId(), formateador.format(c.getFecha()), c.getVehiculo().getDescripcion(), c.getLitros_cargados()
            };
            modelo_de_tabla.addRow(fila);
        }
        tabla.setModel(modelo_de_tabla);
        iniciarModeloColumna();
        
    }
    
    private void iniciarModeloColumna(){
        TableColumnModel columnModel = tabla.getColumnModel();
        columnModel.getColumn(0).setPreferredWidth(6);
        columnModel.getColumn(1).setPreferredWidth(6);
        tabla.setColumnModel(columnModel);
        
    }
    
    private boolean verificarValor(String text) {       
        return (text.matches("[+]?\\d*(\\\\d+)?") && text.equals("")==false);
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton bt_buscar;
    private javax.swing.JButton bt_reimprimir_selecionado;
    private javax.swing.JButton bt_reimprimir_ultimo;
    private org.freixas.jcalendar.JCalendarCombo fecha_desde;
    private org.freixas.jcalendar.JCalendarCombo fecha_hasta;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tabla;
    private javax.swing.JTextField tf_nro_comprobante;
    // End of variables declaration//GEN-END:variables
}
